package top.xxyu.app.admin.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpException;

import top.xxyu.app.login.service.IUserService;
import top.xxyu.app.pojo.CurrentUser;
import top.xxyu.app.pojo.User;
import top.xxyu.common.util.SFTPUtils;
import top.xxyu.context.LocalContext;
import top.xxyu.file.service.FileUtil;
import top.xxyu.file.service.IFileService;

@Controller
@RequestMapping(value = "/admin/user")
public class UserController {
	@Autowired
	private IFileService fileService;

	@Autowired
	private IUserService userService;

	@RequestMapping(value = "/show")
	public String gallery(Model model) {
		CurrentUser currentUser = LocalContext.getCurrentUser();
		User user = userService.selectByPrimaryKey(currentUser.getId());
		model.addAttribute("user", user);
		return "mobile/admin/user";
	}
	
	@RequestMapping(value = "/table")
	public String table(Model model) {
		CurrentUser currentUser = LocalContext.getCurrentUser();
		User user = userService.selectByPrimaryKey(currentUser.getId());
		model.addAttribute("user", user);
		return "mobile/admin/table";
	}
	@RequestMapping(value = "/form")
	public String form(Model model) {
		CurrentUser currentUser = LocalContext.getCurrentUser();
		User user = userService.selectByPrimaryKey(currentUser.getId());
		model.addAttribute("user", user);
		return "mobile/admin/form";
	}

	@RequestMapping(value = "/save")
	@ResponseBody
	public String save(User user, Model model) {
		userService.updateByPrimaryKeySelective(user);
		return "success";
	}

	@RequestMapping(value = "/saveImg")
	@ResponseBody
	public Map<String, Object> saveImg(@RequestParam("file") MultipartFile multiparFile) {
		String msg = "";
		Map<String, Object> map = new HashMap<>();
		if (!multiparFile.isEmpty()) {
			Map<String, Object> mapFile = FileUtil.getFileFromMultiparFile(multiparFile);
			// 获取文件名
			String realName = String.valueOf(mapFile.get(FileUtil.REAL_NAME));

			// 文件名字
			String fileUuidName = String.valueOf(mapFile.get(FileUtil.FILE_UUID_NAME));
			// 文件路径
			String filePath = String.valueOf(mapFile.get(FileUtil.FILE_PATH));

			File file = (File) mapFile.get(FileUtil.FILE);
			try {
				multiparFile.transferTo(file);
				try {
					SFTPUtils.uploadFile(filePath);
				} catch (SftpException e) {
					e.printStackTrace();
				} catch (JSchException e) {
					e.printStackTrace();
				}

				String id = fileService.insertUpload(fileUuidName, realName, filePath);
				User user = new User();
				user.setPhoto(fileUuidName);
				user.setUid(LocalContext.getCurrentUser().getId());
				userService.updateByPrimaryKeySelective(user);
				msg = "上传成功";
				map.put("id", id);
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			msg = "文件为空";
		}
		map.put("msg", msg);
		return map;
	}
}
